package com.example.controller.config.security;

import com.example.controller.config.security.filter.CheckTokenFilter;
import com.example.controller.config.security.handler.AnonymousAuthenticationHandler;
import com.example.controller.config.security.handler.CustomerAccessDeniedHandler;
import com.example.controller.config.security.handler.LoginFailureHandler;
import com.example.controller.config.security.handler.LoginSuccessHandler;
import com.example.controller.config.security.service.CustomerUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AnonymousAuthenticationHandler anonymousAuthenticationHandler;

    @Autowired
    private CustomerAccessDeniedHandler customerAccessDeniedHandler;

    @Autowired
    private LoginFailureHandler loginFailureHandler;

    @Autowired
    private LoginSuccessHandler loginSuccessHandler;

    @Autowired
    private CustomerUserDetailsService customerUserDetailsService;

    @Autowired
    private CheckTokenFilter checkTokenFilter;

    /**
     * 注入加密类
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


    /**
     * 处理登录认证
     * 项目启动时调用
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //登录前校验
        http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);

//        //请求授权的规则
//        http.authorizeRequests()
//                //所有人都可以访问 "/umsAdmin/login"
//                .antMatchers("/umsAdmin/test").permitAll();


        //没有权限默认会跳到登陆页  如果有用户名和密码用这个usernameParameter("username").passwordParameter("pwd")
        http.formLogin().loginProcessingUrl("/umsAdmin/login"); //登录请求地址 自定义
        http.formLogin() //表单登录
                .successHandler(loginSuccessHandler)    //认证成功处理器
                .failureHandler(loginFailureHandler)    //认证失败处理器
                .and()
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)//不创建Session
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(anonymousAuthenticationHandler)       //匿名无权限访问
                .accessDeniedHandler(customerAccessDeniedHandler)               //认证用户无权限访问
                .and()
                .cors();  //支持跨域请求
    }


    /**
     * 配置认证处理器
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customerUserDetailsService).passwordEncoder(passwordEncoder());
    }


}